//
// Created by webturing on 2024/1/1.
//

#include <bits/stdc++.h>

using namespace std;
int n;
const int MAXN = 50, INF = 99999;
int dis[MAXN][MAXN];


int main() {


    cin >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
            int t;
            cin >> t;
            if (i == j)dis[i][j] = 0;
            else if (t == 0) {
                dis[i][j] = INF;
            } else {
                dis[i][j] = t;
            }
        }

    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (dis[i][k] + dis[k][j] < dis[i][j]) {
                    dis[i][j] = dis[i][k] + dis[k][j];
                }
            }
        }
    }


    for (int j = 1; j <= n; j++) {
        for (int i = 1; i <= n; i++) {
            if (dis[j][i] >= INF)
                cout << -1 << " ";
            else
                cout << dis[j][i] << " ";
        }
        cout << endl;
    }
}